<?php
// 应用公共文件

function check_limit(array $limit = []){
    $limit['pagesize'] = !empty($limit['pagesize']) ? $limit['pagesize'] : 10;
    $limit['page'] = !empty($limit['page']) ? ($limit['page']-1)*$limit['pagesize'] : 0 ;
    return $limit;
}

function check_sort(array $sort = []){
    if(empty($sort))
    {
        $sort[0] = 'id';
        $sort[1] = 'desc';
    }
    return $sort;
}

function product_grade(string $grade)
{
    //产品等级
    $sproduct_grade = array(
        10001 => 'A',
        10002 => 'B',
        10003 => 'C',
        10004 => 'D',
        'A' => 'A',
        'B' => 'B',
        'C' => 'C',
        'D' => 'D',
    );
    if(!isset($sproduct_grade[$grade])) return null;
    return $sproduct_grade[$grade];
}

function time_search(array $params = [],string $column='',array $where = []){
    $timestamp_begin = !empty($params[$column.'_begin'])?strtotime(date('Y-m-d H:i:s' , strtotime($params[$column.'_begin']))):0;
    $timestamp_end = !empty($params[$column.'_end'])?strtotime(date('Y-m-d H:i:s' , strtotime($params[$column.'_end']))):0;
    if($timestamp_end>0 && $timestamp_begin>0 && $timestamp_end > $timestamp_begin) {
        $where[] = [$column, '>=', $timestamp_begin ];
        $where[] = [$column, '<=', $timestamp_end ];
    }
    return $where;
}
